package azcat.planner.workers;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import azcat.planner.schedule.Schedule;
import azcat.planner.tools.Tool;

public class WorkforceManager {	
	private ArrayList<Tool> neededTools;
	private ArrayList<Worker> workforce;
	
	public ArrayList<Worker> getWorkforce() {
		return workforce;
	}

	public void setWorkforce(ArrayList<Worker> workforce) {
		this.workforce = workforce;
	}

	public ArrayList<Tool> getNeededTools() {
		return neededTools;
	}

	public WorkforceManager(ArrayList<Tool> neededTools){
		this.neededTools = neededTools;
	}
	/**
	 * This method will call the estimator and do the estimations
	 */
	public ArrayList<Worker> estimateWorkforce(){
		traverseSelectedTools();
		NeededWorkforceEstimator wfe = new NeededWorkforceEstimator(neededTools);
		workforce = wfe.assignWorkers();
		//System.out.println("size: "+workforce.size());
		return this.workforce;
	}
	
	private void traverseSelectedTools(){
		for(int i = 0; i< neededTools.size(); i++){
			getToolsRestrictions(neededTools.get(i));
		}
	}
	/**
	 * Gets the maximum workers per tools for the selected tools
	 * @param tool
	 */
	private void getToolsRestrictions(Tool tool){
		String[] info;
		BufferedReader inputStream = null;
		try {
            inputStream = new BufferedReader(new FileReader("toolsrestrictions.csv"));
            String l;
            while ((l = inputStream.readLine()) != null) {
                info = l.split(",");                
                if(info[0].equals(tool.getName())){
                	tool.setMaxWorkers(Integer.parseInt(info[1]));
                	
                }
            }            
		}
		catch(IOException e){	}
		finally {
            if (inputStream != null) {
                try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
		}
	}

}
